package leetcode;

public class LC52 {
    public int[] searchRange(int[] nums, int target) {
        int l = 0;
        int r = nums.length - 1;
        while(l <= r){
            int i = (l+r)/2;
            if (nums[i] == target) {l = i;r=i;break;}
            if (nums[i] > target) r = --i;
            else l = ++i;
        }
        if (l != r) return new int[]{-1,-1};
        for (int i = 0; i < nums.length / 2; i++) {
            if (l != 0 && nums[l-1] == target) l--;
            if (r != nums.length - 1 &&nums[r+1] == target) r++;
        }
        return new int[]{l,r};
    }
}
